// index_pkg/pages/map/index.js
import QQMAP from '../../../utils/qqmap'
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    latitude: 30.59982,
    longitude: 114.3096,
    scale: 18,
    markers: [
      {
        id: 1,
        latitude: 30.69982,
        longitude: 114.4096,
        width: 24,
        height: 30,
      },
      {
        id: 2,
        latitude: 30.59982,
        longitude: 114.3096,
        iconPath: '/static/images/marker.png',
        width: 40,
        height: 40,
      }],
    polyline: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    async showMe() {
      const res = await wx.getLocation()
      this.setData({
        latitude: res.latitude,
        longitude: res.longitude
      })
    },
    goHome() {
      const t = wx.createMapContext("myMap", this)
      t.openMapApp({
        "longitude": 112.8993,
        "latitude": 30.40147,
        "destination": "潜江市"
      })
    },
  getPolyline() {
    QQMAP.direction({
      mode: 'bicycling',
      from: '30.59982,114.3096',
      to: '30.69982,114.4096',
      success: (res) => {
        // console.log(res);
        var ret = res;
        var coors = ret.result.routes[0].polyline, pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        var kr = 1000000;
        for (var i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (var i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        // console.log(pl)
        //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
        this.setData({
          latitude: pl[0].latitude,
          longitude: pl[0].longitude,
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width: 2
          }]
        })
      }
    })
  }
}
})
